<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Redis基础入门 | 指尖改变世界</title><meta name="keywords" content="Redis"><meta name="author" content="沙屿沫"><meta name="copyright" content="沙屿沫"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="什么是RedisRedis是远程的，基于内存的，非关系型数据库  Redis 应用场景1、缓存缓存数据 2、队列list 实现队列 3、数据存储  Redis 安装1、服务器环境： Linux &#x2F; Centos  2、Redis 3、预装 gcc tclyum install gcc-c++ 【安装步骤】1、下载 Rediswget http:&#x2F;&#x2F;download.redis.io&#x2F;release">
<meta property="og:type" content="article">
<meta property="og:title" content="Redis基础入门">
<meta property="og:url" content="http://blog.shayvmo.cn/2019/09/02/redis-basic/index.html">
<meta property="og:site_name" content="指尖改变世界">
<meta property="og:description" content="什么是RedisRedis是远程的，基于内存的，非关系型数据库  Redis 应用场景1、缓存缓存数据 2、队列list 实现队列 3、数据存储  Redis 安装1、服务器环境： Linux &#x2F; Centos  2、Redis 3、预装 gcc tclyum install gcc-c++ 【安装步骤】1、下载 Rediswget http:&#x2F;&#x2F;download.redis.io&#x2F;release">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://blog.shayvmo.cn/images/redis/redis.jpg">
<meta property="article:published_time" content="2019-09-02T14:20:12.000Z">
<meta property="article:modified_time" content="2019-09-14T08:39:26.000Z">
<meta property="article:author" content="沙屿沫">
<meta property="article:tag" content="Redis">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://blog.shayvmo.cn/images/redis/redis.jpg"><link rel="shortcut icon" href="/images/avatar.jpg"><link rel="canonical" href="http://blog.shayvmo.cn/2019/09/02/redis-basic/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?5e4d1e8fcc524b4e732ea58fa9fa15f0";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Redis基础入门',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2019-09-14 16:39:26'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><meta name="generator" content="Hexo 5.4.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src= "" data-lazy-src="/images/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">74</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">42</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/images/redis/redis.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">指尖改变世界</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Redis基础入门</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2019-09-02T14:20:12.000Z" title="发表于 2019-09-02 22:20:12">2019-09-02</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2019-09-14T08:39:26.000Z" title="更新于 2019-09-14 16:39:26">2019-09-14</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Redis/">Redis</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.5k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>7分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Redis基础入门"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h3 id="什么是Redis"><a href="#什么是Redis" class="headerlink" title="什么是Redis"></a>什么是Redis</h3><p>Redis是远程的，基于内存的，非关系型数据库</p>
<hr>
<h3 id="Redis-应用场景"><a href="#Redis-应用场景" class="headerlink" title="Redis 应用场景"></a>Redis 应用场景</h3><p>1、缓存<br>缓存数据</p>
<p>2、队列<br>list 实现队列</p>
<p>3、数据存储</p>
<hr>
<h3 id="Redis-安装"><a href="#Redis-安装" class="headerlink" title="Redis 安装"></a>Redis 安装</h3><p>1、服务器环境： Linux / Centos </p>
<p>2、Redis</p>
<p>3、预装 gcc tcl<br><code>yum install gcc-c++</code></p>
<p>【安装步骤】<br>1、下载 Redis<br><code>wget http://download.redis.io/releases/redis-5.0.5.tar.gz</code><br><img src= "" data-lazy-src="/images/redis/ff9223e900dd1c663d613e1f671e17f.png" alt=""></p>
<p>2、解压 Redis<br><code>tar -xf redis-5.0.5.tar.gz</code><br>解压后，进入源码目录，执行 <code>make</code> 命令。<br>安装完后页面：<br><img src= "" data-lazy-src="/images/redis/d138e2684b43882f924a5d17fef569e.png" alt=""></p>
<p>3、将 <code>src</code> 目录下二进制文件安装到 <code>/usr/local/bin</code> 下。<br>执行命令 <code>sudo make install</code><br><img src= "" data-lazy-src="/images/redis/730f11238c5853d6e64bde5a413ec77.png" alt=""></p>
<p>4、配置项<br>一般 redis 参考的默认配置文件为解压目录下的 redis.conf ,先把该文件复制到其他目录下，并对其进行配置项修改。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">daemonize no //改为yes ,配置redis server 后台进程启动 </span><br><span class="line"></span><br><span class="line">port 6379 //默认端口 7200 </span><br></pre></td></tr></table></figure>

<p>5、启动 Redis server</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">/usr/local/bin/redis-server /opt/redis/config/redis.conf //以配置项启动redis-server</span><br><span class="line">ps aux|grep redis-server //查看进程是否开启成功</span><br><span class="line">kill -9 XX  //杀死进程</span><br></pre></td></tr></table></figure>
<p><img src= "" data-lazy-src="/images/redis/a9d31d4e367272e802470ab81a35cb1.png" alt=""></p>
<hr>
<p>Redis 客户端</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">#redis-cli --help </span><br><span class="line">redis-cli -h 127.0.0.1 -p 7200</span><br><span class="line"></span><br><span class="line">[root@izwz94ryybsa2lcqvewvzqz redis-5.0.5]# redis-cli -h 127.0.0.1 -p 7200</span><br><span class="line">127.0.0.1:7200&gt; info</span><br></pre></td></tr></table></figure>

<hr>
<h3 id="Redis-数据类型以及各数据类型的使用场景"><a href="#Redis-数据类型以及各数据类型的使用场景" class="headerlink" title="Redis 数据类型以及各数据类型的使用场景"></a>Redis 数据类型以及各数据类型的使用场景</h3><table>
<thead>
<tr>
<th>数据类型</th>
<th>存储的值</th>
<th>读写能力</th>
</tr>
</thead>
<tbody><tr>
<td>String</td>
<td>字符串、整数或浮点，统称为元素</td>
<td>对字符串操作<br>对整数类型加减</td>
</tr>
<tr>
<td>List</td>
<td>序列集合且每个节点包含一个元素</td>
<td>序列两端推入、或弹出元素<br>修建、查找或移除元素</td>
</tr>
<tr>
<td>Set</td>
<td>各不相同的元素</td>
<td>从集合中插入或删除元素</td>
</tr>
<tr>
<td>Hash</td>
<td>有key-value 的散列组<br>key 是字符串，value 是元素</td>
<td>按照key 进行增加删除</td>
</tr>
<tr>
<td>Sort Set</td>
<td>带分数的score-value有序集合<br>score 为浮点，value 是元素</td>
<td>集合插入，按照分数范围查找</td>
</tr>
</tbody></table>
<p>【String】<br>key | value(string/int/float)<br><img src= "" data-lazy-src="/redis/redis/4c58fc89b8ed8e31c9a2b1ebe9ac0ce.png" alt=""></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">127.0.0.1:7200&gt; set string1 eric</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:7200&gt; get string1</span><br><span class="line">&quot;eric&quot;</span><br><span class="line">127.0.0.1:7200&gt; set string2 4</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:7200&gt; get string2</span><br><span class="line">&quot;4&quot;</span><br><span class="line">127.0.0.1:7200&gt; incr string2</span><br><span class="line">(integer) 5</span><br><span class="line">127.0.0.1:7200&gt; get string2</span><br><span class="line">&quot;5&quot;</span><br><span class="line">127.0.0.1:7200&gt; decr string2</span><br><span class="line">(integer) 4</span><br><span class="line">127.0.0.1:7200&gt; decrby string2 2</span><br><span class="line">(integer) 2</span><br></pre></td></tr></table></figure>


<p>【List】<br>列表，元素不唯一<br><img src= "" data-lazy-src="/images/redis/2d2704b069f92c9f0e89596b271c9ad.png" alt=""></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">lpush list1 12 // 12</span><br><span class="line">lpush list1 13 // 13 12</span><br><span class="line"></span><br><span class="line">lindex list1 0 // 13</span><br><span class="line"></span><br><span class="line">rpop list1 //12</span><br><span class="line">#先进先出</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<p>【Set】<br>集合，元素唯一<br><img src= "" data-lazy-src="/images/redis/632cac3d6180e19518df403091d6c89.png" alt=""></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">sadd set1 redis</span><br><span class="line">sadd set1 memcached</span><br><span class="line">smembers set1</span><br></pre></td></tr></table></figure>


<p>【Hash】<br>哈希<br><img src= "" data-lazy-src="/images/redis/befd56703b38b25cc721c3438da93f0.png" alt=""></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">hset key field value # 将哈希表 key 中的字段 field 的值设为 value 。</span><br></pre></td></tr></table></figure>


<p>【Sort Set】<br>有序集合<br><img src= "" data-lazy-src="/images/redis/7d5febbd51ba8db7848c7bc7fd3b2f1.png" alt=""></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">ZADD key score1 member1 [score2 member2]  # 向有序集合添加一个或多个成员，或者更新已存在成员的分数</span><br></pre></td></tr></table></figure>



<h3 id="操作-Redis"><a href="#操作-Redis" class="headerlink" title="操作 Redis"></a>操作 Redis</h3><p><strong>【服务器安装要求】</strong></p>
<p>1、Linux 服务器 （这里使用 Centos ）</p>
<p>2、PHP 5.3 （phpize / php-config ） 以上，具体和下载的 redis 要求版本一致</p>
<p>3、PHP redis 拓展</p>
<p><img src= "" data-lazy-src="/images/redis/ed7a32accfa66e6070c2af2d33acfc5.png" alt=""></p>
<p>下载php-redis 拓展</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">wget https://github.com/phpredis/phpredis/archive/develop.zip</span><br></pre></td></tr></table></figure>

<p><img src= "" data-lazy-src="/images/redis/929c8c5d55edc8e56e6a93baba839e5.png" alt=""></p>
<p>make 出现错误</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">[root@izwz94ryybsa2lcqvewvzqz phpredis-develop]# make</span><br><span class="line">/bin/sh /opt/phpredis-develop/libtool --mode=compile cc  -I. -I/opt/phpredis-develop -DPHP_ATOM_INC -I/opt/phpredis-develop/include -I/opt/phpredis-develop/main -I/opt/phpredis-develop -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/include/php/ext  -DHAVE_CONFIG_H  -g -O2   -c /opt/phpredis-develop/redis.c -o redis.lo</span><br><span class="line">libtool: compile:  cc -I. -I/opt/phpredis-develop -DPHP_ATOM_INC -I/opt/phpredis-develop/include -I/opt/phpredis-develop/main -I/opt/phpredis-develop -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/include/php/ext -DHAVE_CONFIG_H -g -O2 -c /opt/phpredis-develop/redis.c  -fPIC -DPIC -o .libs/redis.o</span><br><span class="line">In file included from /opt/phpredis-develop/redis.c:27:0:</span><br><span class="line">/opt/phpredis-develop/common.h:12:28: fatal error: zend_smart_str.h: No such file or directory</span><br><span class="line"> #include &lt;zend_smart_str.h&gt;</span><br><span class="line">                            ^</span><br><span class="line">compilation terminated.</span><br><span class="line">make: *** [redis.lo] Error 1</span><br></pre></td></tr></table></figure>

<p>make 出现错误，是因为下载的 redis 版本要求的PHP版本 和服务器的版本不一致导致的。最新版的redis，最低要求 PHP7.0 或以上。<br>原先服务器用的是php5.6，于是升级到php 7.1。首先卸载5.6 <code>yum remove php-common</code><br>然后再安装PHP7.1</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">yum install -y php71w-fpm php71w-opcache php71w-cli php71w-gd php71w-imap </span><br><span class="line">php71w-mysqlnd php71w-mbstring php71w-mcrypt php71w-pdo php71w-pecl-apcu php71w-pecl-mongodb </span><br><span class="line">php71w-pecl-redis php71w-pgsql php71w-xml php71w-xmlrpc php71w-devel mod_php71w</span><br></pre></td></tr></table></figure>

<p>make &amp;&amp; make install 成功<br><img src= "" data-lazy-src="/images/redis/7f14962bd924225f8e15b90068ec6a3.png" alt=""></p>
<p>下一步，更改 php.ini 文件，加载 redis.so 模块<br><code>extention=redis.so</code></p>
<p>使用命令<code>php -m</code> 查看加载的模块，即可看到已经加载 redis</p>
<hr>
<p>【PHP 如何连接 redis 】<br>phpredis 文档<br><code>https://github.com/phpredis/phpredis</code></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">$redis = new \Redis();</span><br><span class="line">$redis-&gt;connect(&#x27;127.0.0.1&#x27;,7200);</span><br><span class="line"></span><br><span class="line">//string 操作，保存json</span><br><span class="line">$redis-&gt;delete(&quot;string1&quot;);</span><br><span class="line"></span><br><span class="line">$redis-&gt;set(&quot;string1&quot;,&quot;val1&quot;);</span><br><span class="line"></span><br><span class="line">$val = $redis-&gt;get(&quot;string1&quot;);</span><br><span class="line"></span><br><span class="line">var_dump($val);</span><br><span class="line"></span><br><span class="line">$redis-&gt;set(&quot;string1&quot;,4);</span><br><span class="line"></span><br><span class="line">$redis-&gt;incr(&quot;string1&quot;,2);</span><br><span class="line"></span><br><span class="line">$val = $redis-&gt;get(&quot;string1&quot;);</span><br><span class="line"></span><br><span class="line">var_dump($val);</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line">//list 实现队列，先进先出，异步行为</span><br><span class="line">$redis-&gt;delete(&quot;list1&quot;);</span><br><span class="line"></span><br><span class="line">$redis-&gt;lPush(&quot;list1&quot;,&quot;A&quot;);</span><br><span class="line"></span><br><span class="line">$redis-&gt;lPush(&quot;list1&quot;,&quot;B&quot;);</span><br><span class="line"></span><br><span class="line">$redis-&gt;lPush(&quot;list1&quot;,&quot;C&quot;);</span><br><span class="line"></span><br><span class="line">$val = $redis-&gt;rPop(&quot;list1&quot;);</span><br><span class="line"></span><br><span class="line">var_dump($val);//输出A</span><br></pre></td></tr></table></figure>


<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">//set 唯一集合</span><br><span class="line">$redis-&gt;delete(&quot;set1&quot;);</span><br><span class="line"></span><br><span class="line">$redis-&gt;sAdd(&quot;set1&quot;,&quot;A&quot;);</span><br><span class="line"></span><br><span class="line">$redis-&gt;sAdd(&quot;set1&quot;,&quot;B&quot;);</span><br><span class="line"></span><br><span class="line">$redis-&gt;sAdd(&quot;set1&quot;,&quot;C&quot;);</span><br><span class="line"></span><br><span class="line">$redis-&gt;sAdd(&quot;set1&quot;,&quot;C&quot;);</span><br><span class="line"></span><br><span class="line">$val = $redis-&gt;sCard(&quot;set1&quot;);</span><br><span class="line">var_dump($val);</span><br><span class="line"></span><br><span class="line">var_dump($redis-&gt;sMembers(&quot;set1&quot;));</span><br></pre></td></tr></table></figure>


<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">//hash 一般用于存储复杂属性</span><br><span class="line"></span><br><span class="line">$redis-&gt;delete(&quot;driver1&quot;);</span><br><span class="line"></span><br><span class="line">$redis-&gt;hSet(&quot;driver1&quot;,&quot;name&quot;,&quot;shayvmo&quot;);</span><br><span class="line"></span><br><span class="line">$redis-&gt;hSet(&quot;driver1&quot;,&quot;age&quot;,25);</span><br><span class="line"></span><br><span class="line">$redis-&gt;hSet(&quot;driver1&quot;,&quot;gender&quot;,1);</span><br><span class="line"></span><br><span class="line">var_dump($redis-&gt;hGet(&quot;driver1&quot;,&quot;name&quot;);</span><br><span class="line"></span><br><span class="line">var_dump($redis-&gt;hMGet(&quot;driver1&quot;,&quot;name&quot;,&quot;age&quot;);</span><br></pre></td></tr></table></figure>



<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">// sort set 排行榜</span><br><span class="line">// zAdd  zRange 分数低到高  zRevRange 高到低</span><br><span class="line"></span><br><span class="line">$redis-&gt;delete(&quot;zset1&quot;);</span><br><span class="line"></span><br><span class="line">$redis-&gt;zAdd(&quot;zset1&quot;,100,&#x27;shayvmo&#x27;);</span><br><span class="line">$redis-&gt;zAdd(&quot;zset1&quot;,88,&#x27;shayvmo88&#x27;);</span><br><span class="line">$redis-&gt;zAdd(&quot;zset1&quot;,97,&#x27;shayvmo97&#x27;);</span><br><span class="line">$redis-&gt;zAdd(&quot;zset1&quot;,93,&#x27;shayvmo93&#x27;);</span><br><span class="line"></span><br><span class="line">var_dump($redis-&gt;zRange(&quot;zset1&quot;,0,-1));//分数低到高全部返回</span><br><span class="line"></span><br><span class="line">var_dump($redis-&gt;zRevRange(&quot;zset1&quot;,0,-1));//分数高到低全部返回</span><br><span class="line"></span><br><span class="line">var_dump($redis-&gt;zRevRange(&quot;zset1&quot;,0,-1,true));//分数高到低全部返回，并返回分数</span><br></pre></td></tr></table></figure>



<p>【注意事项】<br>运行PHP 文件时，出错</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">PHP Startup: Unable to load dynamic library &#x27;/usr/lib64/php/modules/redis.so&#x27; </span><br><span class="line">- /usr/lib64/php/modules/redis.so: undefined symbol: php_json_decode_ex in Unknown on line 0</span><br></pre></td></tr></table></figure>
<p>解决方法： 去掉<code>php.ini</code> 中的 <code>extension=redis.so</code><br>来源链接：<a target="_blank" rel="noopener" href="https://stackoverflow.com/questions/41052999/php-warning-unable-to-load-dynamic-library-usr-lib64-php-modules-solr-so-und">https://stackoverflow.com/questions/41052999/php-warning-unable-to-load-dynamic-library-usr-lib64-php-modules-solr-so-und</a></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">沙屿沫</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://blog.shayvmo.cn/2019/09/02/redis-basic/">http://blog.shayvmo.cn/2019/09/02/redis-basic/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://blog.shayvmo.cn" target="_blank">指尖改变世界</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Redis/">Redis</a></div><div class="post_share"><div class="social-share" data-image="/images/redis/redis.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2019/09/09/migrate/"><img class="prev-cover" src= "" data-lazy-src="/images/code.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Hexo 迁移到另外一台电脑 【亲测】</div></div></a></div><div class="next-post pull-right"><a href="/2019/08/30/memcache/"><img class="next-cover" src= "" data-lazy-src="/images/7e3483ec4f7dedd5a30f0e7fee1635b921.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Centos 配置 PHP memcached 缓存【踩坑】</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src= "" data-lazy-src="/images/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">沙屿沫</div><div class="author-info__description">科技始终来源于人性</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">74</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">42</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/shayvmo"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">生活就像海洋，只有意志坚强的人才能到达彼岸。</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AFRedis"><span class="toc-number">1.</span> <span class="toc-text">什么是Redis</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Redis-%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-number">2.</span> <span class="toc-text">Redis 应用场景</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Redis-%E5%AE%89%E8%A3%85"><span class="toc-number">3.</span> <span class="toc-text">Redis 安装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Redis-%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E4%BB%A5%E5%8F%8A%E5%90%84%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E7%9A%84%E4%BD%BF%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-number">4.</span> <span class="toc-text">Redis 数据类型以及各数据类型的使用场景</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%93%8D%E4%BD%9C-Redis"><span class="toc-number">5.</span> <span class="toc-text">操作 Redis</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/06/05/webman-route-annotation/" title="webman 框架 注解路由插件"><img src= "" data-lazy-src="/images/code.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="webman 框架 注解路由插件"/></a><div class="content"><a class="title" href="/2023/06/05/webman-route-annotation/" title="webman 框架 注解路由插件">webman 框架 注解路由插件</a><time datetime="2023-06-05T07:04:07.000Z" title="发表于 2023-06-05 15:04:07">2023-06-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/05/29/nginx-reload-not-effect/" title="windows服务器重载nginx配置无效"><img src= "" data-lazy-src="/images/code.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="windows服务器重载nginx配置无效"/></a><div class="content"><a class="title" href="/2023/05/29/nginx-reload-not-effect/" title="windows服务器重载nginx配置无效">windows服务器重载nginx配置无效</a><time datetime="2023-05-29T08:03:45.000Z" title="发表于 2023-05-29 16:03:45">2023-05-29</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/05/24/php-aes/" title="前后端接口传输AES加密"><img src= "" data-lazy-src="/images/code.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="前后端接口传输AES加密"/></a><div class="content"><a class="title" href="/2023/05/24/php-aes/" title="前后端接口传输AES加密">前后端接口传输AES加密</a><time datetime="2023-05-24T07:00:39.000Z" title="发表于 2023-05-24 15:00:39">2023-05-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/08/16/yar-demo/" title="Yar拓展，实现PHP的RPC服务"><img src= "" data-lazy-src="/images/code.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Yar拓展，实现PHP的RPC服务"/></a><div class="content"><a class="title" href="/2022/08/16/yar-demo/" title="Yar拓展，实现PHP的RPC服务">Yar拓展，实现PHP的RPC服务</a><time datetime="2022-08-16T09:30:43.000Z" title="发表于 2022-08-16 17:30:43">2022-08-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/08/15/my-online-log/" title="线上日志系统"><img src= "" data-lazy-src="/images/code.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="线上日志系统"/></a><div class="content"><a class="title" href="/2022/08/15/my-online-log/" title="线上日志系统">线上日志系统</a><time datetime="2022-08-15T09:03:01.000Z" title="发表于 2022-08-15 17:03:01">2022-08-15</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/images/redis/redis.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2019 - 2023 By 沙屿沫</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><a href="http://www.beian.miit.gov.cn/" target="_blank" rel="noopener"><img class="icp-icon" src= "" data-lazy-src="/images/icp.png"><span>粤ICP备18133330号</span></a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="false"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-show-text.min.js" data-mobile="false" data-text="富强,民主,文明,和谐,自由,平等,公正,法治,爱国,敬业,诚信,友善" data-fontsize="15px" data-random="false" async="async"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>